#include "StdAfx.h"

MsgBox::MsgBox(ui32 x, ui32 y, ui32 sizeX, ui32 sizeY, std::string szTitle, std::string szText)
{
	m_dialog = new Dialog(x, y, sizeX, sizeY, szTitle);
	m_dialog->m_visible = false;
	m_dialog->AddButton(this, 20, 70, "Yes", &MsgBox::OnYesButton);
	m_dialog->AddButton(this, sizeX * 32 - 90, 70, "No", &MsgBox::OnNoButton);
	std::string::size_type t = szText.find('\n');
	if(t == std::string::npos)
		m_dialog->AddText(20, 30, szText);
	sUI.AddElement(m_dialog);
}

MsgBox::~MsgBox()
{
	sUI.RemoveElement(m_dialog);
	delete m_dialog;
}

ui32 MsgBox::Show(bool blocking)
{
	m_dialog->m_visible = true;
	if(!blocking)
		return 0;
	while(true)
	{
		volatile long value = m_dialog->m_visible;
		long new_val = m_dialog->m_visible;
		bool vis = InterlockedCompareExchange((volatile LONG*)(&value), new_val, true) > 0;
		if(!vis)
			break;
		Sleep(10);
	}
	return m_return;
}

void MsgBox::OnYesButton(UIButton<MsgBox>* pButton)
{
	m_return = 1;
	m_dialog->m_visible = false;
}

void MsgBox::OnNoButton(UIButton<MsgBox>* pButton)
{
	m_return = 2;
	m_dialog->m_visible = false;
}